package org.example.news.controller;
import org.apache.ibatis.annotations.Delete;
import org.example.news.pojo.Article;
import org.example.news.pojo.Category;
import org.example.news.pojo.PageBean;
import org.example.news.pojo.Result;
import org.example.news.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //新增文章
    @PostMapping()
    public Result add(@RequestBody @Validated Article article){

        articleService.add(article);
        return Result.success();
    }
    //文章列表（条件分页）
    @GetMapping
    public  Result<PageBean<Article>>  list(
            Integer pageNum,
            Integer pageSize,
           @RequestParam(required = false) Integer categoryId,   //不是必须的参数
            @RequestParam(required = false) String state
            ){
        PageBean<Article> pb=articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);

    }
    //获取文章详情
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        Article  a=articleService.findById(id);
        return Result.success(a);

    }


    //更新文章
    @PutMapping()
    public Result update(@RequestBody @Validated(Category.Update.class) Article article){
        articleService.update(article);
        return Result.success();

    }


    //删除文章
    @DeleteMapping()
    public Result  delete(Integer id){
        articleService.deleteById(id);
        return Result.success();
    }
}
